package com.star.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.star.pojo.Blog;
import com.star.pojo.User;
import com.star.service.impl.BlogServiceImpl;
import com.star.service.impl.TagServiceImpl;
import com.star.service.impl.TypeServiceImpl;
import com.star.vo.BlogVo;
import com.star.vo.SearchBlog;
import com.star.vo.ShowBlog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.List;

/**
 * com.star.controller.admin
 *
 * @Description: 博客管理控制器
 * @Date: 2020-08-05 20:14
 * @Author: LENSTAR
 * @URL: http://123.57.161.114:9000/
 */
@Controller
@RequestMapping("/admin/blogs")
public class BlogController {

    @Autowired
    private TypeServiceImpl typeService;
    @Autowired
    private BlogServiceImpl blogService;
    @Autowired
    private TagServiceImpl tagService;

    //跳转到博客新增页面
    @GetMapping("/input")
    public String input(Model model) {
        model.addAttribute("types", typeService.getAllType());
        model.addAttribute("tags", tagService.getAllTag());
        model.addAttribute("blog", new Blog());
        return "admin/blogs-input";
    }

    //博客新增
    @PostMapping
    public String post(Blog blog, RedirectAttributes attributes, HttpSession session) {
        //新增的时候需要传递blog对象，blog对象需要有user
        blog.setUser((User) session.getAttribute("user"));
        //根据前端传过来的分类设置blog的分类
        blog.setType(typeService.getType(blog.getType().getId()));
        //设置blog中typeId属性
        blog.setTypeId(blog.getType().getId());
        //设置blog中的用户id
        blog.setUserId(blog.getUser().getId());

        int save = blogService.saveBlog(blog);
        if (save == 1) {
            attributes.addFlashAttribute("message", "新增成功！");
        } else {
            attributes.addFlashAttribute("message", "新增失败！");
        }
        return "redirect:/admin/blogs";
    }

    //博客列表
    @RequestMapping
    public String blogs(Model model, @RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum) {
        //按照排序字段 排序
        String orderBy = "update_time desc";//根据更新时间排序
        PageHelper.startPage(pageNum, 10, orderBy);//分页规则
        List<BlogVo> list = blogService.getAllBlog();//获取数据
        PageInfo<BlogVo> pageInfo = new PageInfo<>(list);//数据分页后的详细信息
        model.addAttribute("types", typeService.getAllType());//将信息传给前端
        model.addAttribute("tags", tagService.getAllTag());//将信息传给前端
        model.addAttribute("pageInfo", pageInfo);
        return "admin/blogs";
    }

    //根据标题和分类查询博客
    @PostMapping("/query")
    public String queryBlog(Model model, SearchBlog searchBlog, @RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum) {
        //按照排序字段 排序
        String orderBy = "update_time desc";//根据更新时间排序
        PageHelper.startPage(pageNum, 10, orderBy);//分页规则
        List<BlogVo> list = blogService.searchByTitleAndType(searchBlog);//获取数据
        PageInfo<BlogVo> pageInfo = new PageInfo<>(list);//数据分页后的详细信息
        model.addAttribute("pageInfo", pageInfo);
        return "admin/blogs :: blogList";//更新blogList片段
    }

    //删除博客
    @GetMapping("/{id}/delete")
    public String deleteBlog(@PathVariable("id") Long id, RedirectAttributes attributes) {
        int delete = blogService.deleteBlog(id);
        if (delete == 1) {
            attributes.addFlashAttribute("message", "删除成功！");
        } else {
            attributes.addFlashAttribute("message", "删除失败！");
        }
        return "redirect:/admin/blogs";
    }

    //跳转到修改博客页面
    @GetMapping("/{id}/input")
    public String update(@PathVariable("id") Long id, Model model) {
        ShowBlog showBlog = blogService.getShowBlogById(id);
        model.addAttribute("types", typeService.getAllType());
        model.addAttribute("tags", tagService.getAllTag());
        model.addAttribute("blog",showBlog);
        return "admin/blogs-input";
    }

    //修改博客
    @PostMapping("/{id}")
    public String updatePost(@Valid ShowBlog showBlog, @PathVariable("id") Long id, RedirectAttributes attributes) {
        int update = blogService.updateBlog(showBlog);
        if (update == 1){
            attributes.addFlashAttribute("message", "修改成功！");
        } else {
            attributes.addFlashAttribute("message", "修改失败！");
        }
        return "redirect:/admin/blogs";
    }
}